<?php

/**
 * action actions.
 *
 * @package    s-test
 * @subpackage action
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class actionActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->ktd_actions = Doctrine::getTable('KTDAction')
      ->createQuery('a')
      ->execute();
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->getGuardUser()->hasActualCharacter(), "Vous n'avez pas de personnage selectionné !");
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('post_action'), 'Vous ne pouvez éxecuter d\'actions. :(');
    $this->forward404Unless($adventure_id = $request->getParameter(('id')));
    $this->forward404Unless($this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array('id' => $request->getParameter('id'))), sprintf("L'aventure où vous souhaitez répondre n'existe pas ou plus."));
    $this->ktd_region = $this->ktd_adventure->getKTDRegion();
    $this->form = new KTDActionForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->forward404Unless($this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array('id' => $request->getParameter('adventure_id'))), sprintf("L'aventure où vous souhaitez répondre n'existe pas ou plus."));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasActualCharacter(), "Vous n'avez pas de personnage selectionné !");
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('post_action'), 'Vous ne pouvez répondre à cette aventure. :(');
    $this->ktd_region = $this->ktd_adventure->getKTDRegion();
    
    $ktd_action = new KTDAction();

    $ktd_action->setAdventureId($this->ktd_adventure->getId());
    $ktd_action->setCharacterId($this->getUser()->getGuardUser()->getActualCharacterId());

    $this->form = new KTDActionForm($ktd_action);

    $this->processForm($request, $this->form, true);
    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($ktd_action = Doctrine::getTable('KTDAction')->find(array($request->getParameter('id'))), sprintf('L\'action que vous souhaitez éditer n\'existe pas ou plus.'));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasRelPermission('edit_action', $ktd_action->getKTDPlayer()), sprintf('Vous ne pouvez pas éditer cette action.'));
    
    $this->form = new KTDActionForm($ktd_action);
    $this->ktd_adventure = $ktd_action->getKTDAdventure();
    $this->ktd_action = $ktd_action;
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($ktd_action = Doctrine::getTable('KTDAction')->find(array($request->getParameter('id'))), sprintf('L\'action que vous souhaitez éditer n\'existe pas ou plus.'));
    $this->forward404Unless($this->getUser()->hasRelPermission('edit_action', $ktd_action->getKTDPlayer()), sprintf('Vous ne pouvez pas éditer cette action.'));
    
    $this->form = new KTDActionForm($ktd_action);
    $this->processForm($request, $this->form);
    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    
    $this->forward404Unless($ktd_action = Doctrine::getTable('KTDAction')->find(array($request->getParameter('id'))), sprintf('L\'action demandée n\'existe pas.'));
    $ktd_adventure = $ktd_action->getKTDAdventure();
    if($ktd_action->isFirstAction()){
        $this->forward404Unless($this->getUser()->hasRelPermission("delete_adventure", $ktd_action->getKTDPlayer()), "Vous ne pouvez pas supprimer d'aventure");
        $ktd_adventure = $ktd_action->getKTDAdventure();
        $ktd_region = $ktd_adventure->getKTDRegion();
        $ktd_adventure->delete();
        $this->redirect($this->generateUrl("show_region", $ktd_region));
    }
    else{
        $this->forwardUnless($this->getUser()->hasRelPermission("delete_action", $ktd_action->getKTDPlayer()), 'default', '505');
        $ktd_action->delete();
        $this->redirect($this->generateUrl("show_adventure", $ktd_adventure));
    }    
  }

  protected function processForm(sfWebRequest $request, sfForm $form, $isNew = false)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $ktd_action = $form->save();
      if($isNew){
            //On envoit l'évenement
            KTDEventManager::getSingleton()->handleAction(KTDActionHandler::$onPost,
                                                  array("player"    => $this->getUser()->getGuardUser(),
                                                        "amount"    => sfConfig::get("app_amount_on_action_post"),
                                                        "adventure" => $ktd_action->getKTDAdventure()));

      }
      
      $this->redirect($this->generateUrl('show_adventure', $ktd_action->getKTDAdventure()));
    }
  }
}
